<?php

/**
 * this class provides some useful functions which are available for all applications
 * @author		simon ungerboeck <ungerboeck_simon@hotmail.com>
 *
 */
class Helper {
	
	/**
	 * returns the database name from config file based on environment
	 * 
	 * @param string $name        	
	 * @return string
	 */
	public static function getDB($name) {
		return \Config::get ( 'database.' . strtolower ( $name ) ) . '.';
	}
	
	
	/**
	 * checks if the last character of the url is a slash, if not add a slash
	 * 
	 * @param string $url        	
	 * @return string
	 */
	public static function addUrlSlash($url) {
		if ($url [count ( $url ) - 1] == '/')
			return $url;
		return $url . '/';
	}
	
	
	/**
	 * adds the text filter to a given sql statement
	 * @param 		string		 $stmt
	 * @param 		array		 $params
	 * @param 		array		 $filterValues
	 * @param 		string		 $filter
	 */
	public static function addTextFilter(&$stmt, &$params, $filterValues, $filter){
		if (isset($filter) and $filter != '') {
			$stmt .= ' and (';
			foreach ($filterValues as $value) {
				$stmt .= 'upper(' . $value . ') like upper(?) or ';
				array_push($params, ('%' . $filter . '%'));
			}
			$stmt = substr($stmt, 0, -4); //remove the last ' or ';
			$stmt .= ') ';
		}
	}
	
	
	/**
	 * returns a div with a std message
	 * @param 		string		 $prefix
	 * @return 		string
	 */
	public static function getSuccessArea($prefix) {
		return '<div id="' . $prefix . 'SUCCESS" class="successArea"><div class="successImg"><img src="' . ADMIN_IMAGES . 'success_big.gif" /></div><div class="successMsg">Daten wurden erfolgreich gespeichert!</div></div>';
	}
	
	
	/**
	 * compares models
	 * @param 		mixed		 $m1
	 * @param 		mixed		 $m2
	 * @param 		array		 $excludeArray
	 * @return boolean
	 */
	public static function compareModels($m1, $m2, $excludeArray = array('id', 'created_at', 'expired_at')) {
		foreach ($m1->getAttributes() as $attribute => $value) {
			if (in_array($attribute, $excludeArray))
				continue;
	
			if (trim($value) !== trim($m2->$attribute)) {
				return false;
			}
		}
		return true;
	}
}
